$(document).ready(function() {
	init();
});

function init() {
	//set up the accordions for each game
	$( "#gameSelectOuter" ).accordion({
		collapsible: true,
		active: false,
		autoHeight: false,
	}).css({width: '90%'});
	//Set up the buttons to look like buttons
	$('.button').button();
	
	//remove the accordian that is applied to new game
	$('#newGame').accordion( "destroy" );
	$('.delete').click(function(){
		clickedObj = this;
		$( "#dialog" ).dialog({
			resizable: true,
			height:'auto',
			width:350,
			modal: true,
			buttons: {
				"Delete game": function() {
					$( this ).dialog( "close" );
					deleteGame($(clickedObj).attr('url'));
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			}
		});
		
		
	});
}


function deleteGame(url){
	$.ajax({url:url,
		type: "GET",
		dataType: 'json',
		success:function(info){
			if (info['success']){
				$('#accordionHeader'+info['id']).parent().remove();
				$('#accordionHeader'+info['id']).remove();
				$('#gameInfo'+info['id']).remove();
			} else {
				$.notifyBar({
					html: 'There was an error sending data',
					delay: 2000,
					animationSpeed: "normal",
					cls: "error"
				});
			}
		},
		error:function(){
			$.notifyBar({
				html: 'Error sending data',
				delay: 2000,
				animationSpeed: "normal",
				cls: "error"
			});
		}
	});
	
}

